package bigbanksystemserver;

import java.awt.Font;
import javax.swing.ImageIcon;

/**
 *
 * @author Wong Kah Chun & Woo Chee Meng
 */
public class ConfirmationDialog extends javax.swing.JDialog {
    //Icon type

    public static final int QUESTION_ICON = 1;
    public static final int INFO_ICON = 2;
    public static final int ERROR_ICON = 3;
    //Selected option type
    public final int YES_OPTION = 1;
    public final int NO_OPTION = 2;
    public final int OK_OPTION = 3;
    //Selected option
    private int selectedOption = 0;

    public ConfirmationDialog(java.awt.Frame parent, boolean modal, int type, String msg) {
        super(parent, modal);
        initComponents();
        setDialogIcon(type);
        setDialogMsg(msg);
        DraggableFrame frame = new DraggableFrame(this, background);
        setLocationRelativeTo(parent);
        setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        confirmationMsg = new javax.swing.JLayeredPane();
        ok_btn = new javax.swing.JButton();
        no_btn = new javax.swing.JButton();
        yes_btn = new javax.swing.JButton();
        msg_txt = new javax.swing.JTextArea();
        icon_label = new javax.swing.JLabel();
        exit_btn = new javax.swing.JButton();
        background = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
        setResizable(false);
        setUndecorated(true);

        ok_btn.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        ok_btn.setForeground(new java.awt.Color(2, 65, 115));
        ok_btn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bigbanksystemserver/images/button_icon_yes.png"))); // NOI18N
        ok_btn.setText("OK");
        ok_btn.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        ok_btn.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        ok_btn.setFocusable(false);
        ok_btn.setMaximumSize(new java.awt.Dimension(80, 30));
        ok_btn.setMinimumSize(new java.awt.Dimension(80, 30));
        ok_btn.setPreferredSize(new java.awt.Dimension(80, 30));
        ok_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ok_btnActionPerformed(evt);
            }
        });
        ok_btn.setBounds(120, 95, 80, 30);
        confirmationMsg.add(ok_btn, javax.swing.JLayeredPane.DEFAULT_LAYER);

        no_btn.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        no_btn.setForeground(new java.awt.Color(2, 65, 115));
        no_btn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bigbanksystemserver/images/button_icon_no.png"))); // NOI18N
        no_btn.setText("No");
        no_btn.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        no_btn.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        no_btn.setFocusable(false);
        no_btn.setMaximumSize(new java.awt.Dimension(80, 30));
        no_btn.setMinimumSize(new java.awt.Dimension(80, 30));
        no_btn.setPreferredSize(new java.awt.Dimension(80, 30));
        no_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                no_btnActionPerformed(evt);
            }
        });
        no_btn.setBounds(170, 95, 80, 30);
        confirmationMsg.add(no_btn, javax.swing.JLayeredPane.DEFAULT_LAYER);

        yes_btn.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        yes_btn.setForeground(new java.awt.Color(2, 65, 115));
        yes_btn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bigbanksystemserver/images/button_icon_yes.png"))); // NOI18N
        yes_btn.setText("Yes");
        yes_btn.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        yes_btn.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        yes_btn.setFocusable(false);
        yes_btn.setMaximumSize(new java.awt.Dimension(80, 30));
        yes_btn.setMinimumSize(new java.awt.Dimension(80, 30));
        yes_btn.setPreferredSize(new java.awt.Dimension(80, 30));
        yes_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                yes_btnActionPerformed(evt);
            }
        });
        yes_btn.setBounds(80, 95, 80, 30);
        confirmationMsg.add(yes_btn, javax.swing.JLayeredPane.DEFAULT_LAYER);

        msg_txt.setColumns(20);
        msg_txt.setEditable(false);
        msg_txt.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        msg_txt.setForeground(new java.awt.Color(255, 255, 255));
        msg_txt.setLineWrap(true);
        msg_txt.setRows(3);
        msg_txt.setText("text here");
        msg_txt.setWrapStyleWord(true);
        msg_txt.setBorder(null);
        msg_txt.setFocusable(false);
        msg_txt.setOpaque(false);
        msg_txt.setBounds(100, 45, 195, 20);
        confirmationMsg.add(msg_txt, javax.swing.JLayeredPane.DEFAULT_LAYER);

        icon_label.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bigbanksystemserver/images/msg_info.png"))); // NOI18N
        icon_label.setBounds(20, 25, 60, 60);
        confirmationMsg.add(icon_label, javax.swing.JLayeredPane.DEFAULT_LAYER);

        exit_btn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bigbanksystemserver/images/icon_exit.png"))); // NOI18N
        exit_btn.setBorderPainted(false);
        exit_btn.setContentAreaFilled(false);
        exit_btn.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        exit_btn.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/bigbanksystemserver/images/icon_move_exit.png"))); // NOI18N
        exit_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exit_btnActionPerformed(evt);
            }
        });
        exit_btn.setBounds(270, 0, 50, 26);
        confirmationMsg.add(exit_btn, javax.swing.JLayeredPane.DEFAULT_LAYER);

        background.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bigbanksystemserver/images/msgBackground.png"))); // NOI18N
        background.setBounds(0, 0, 320, 140);
        confirmationMsg.add(background, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 320, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(confirmationMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 140, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(confirmationMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //Close the dialog
    private void exit_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exit_btnActionPerformed
        dispose();
    }//GEN-LAST:event_exit_btnActionPerformed

    //Assign selectedOption as YES_OPTION if yes_btn is clicked
    private void yes_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_yes_btnActionPerformed
        selectedOption = YES_OPTION;
        dispose();
    }//GEN-LAST:event_yes_btnActionPerformed

    //Assign selectedOption as NO_OPTION if no_btn is clicked
    private void no_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_no_btnActionPerformed
        selectedOption = NO_OPTION;
        dispose();
    }//GEN-LAST:event_no_btnActionPerformed

    //Assign selectedOption as OK_OPTION if ok_btn is clicked
    private void ok_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ok_btnActionPerformed
        selectedOption = OK_OPTION;
        dispose();
    }//GEN-LAST:event_ok_btnActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ConfirmationDialog dialog = new ConfirmationDialog(new javax.swing.JFrame(), true, 0, "");
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    //Setup the dialog icon and buttons based on the type
    private void setDialogIcon(int type) {
        ImageIcon question = new ImageIcon(getClass().getResource("/bigbanksystemserver/images/msg_question.png"));
        ImageIcon info = new ImageIcon(getClass().getResource("/bigbanksystemserver/images/msg_info.png"));
        ImageIcon error = new ImageIcon(getClass().getResource("/bigbanksystemserver/images/msg_error.png"));

        if (type == QUESTION_ICON) {
            icon_label.setIcon(question);
            ok_btn.setVisible(false);
        } else if (type == INFO_ICON) {
            icon_label.setIcon(info);
            yes_btn.setVisible(false);
            no_btn.setVisible(false);
        } else if (type == ERROR_ICON) {
            icon_label.setIcon(error);
            yes_btn.setVisible(false);
            no_btn.setVisible(false);
        }
    }

    //Setup the message of dialog
    private void setDialogMsg(String msg) {
        msg_txt.setText(msg);

        //Adjust the position of text if the message is too long
        if (msg.length() > 50) {
            msg_txt.setFont(new Font("Tahoma", Font.BOLD, 12));
            msg_txt.setLocation(100, 32);
            msg_txt.setSize(195, 60);
        } else if (msg.length() > 30) {
            msg_txt.setFont(new Font("Tahoma", Font.BOLD, 12));
            msg_txt.setLocation(100, 40);
            msg_txt.setSize(195, 40);
        } else {
            msg_txt.setFont(new Font("Tahoma", Font.BOLD, 12));
            msg_txt.setLocation(100, 45);
            msg_txt.setSize(195, 40);
        }
    }

    //Return the selected option
    public int getSelectedOption() {
        return selectedOption;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel background;
    private javax.swing.JLayeredPane confirmationMsg;
    private javax.swing.JButton exit_btn;
    private javax.swing.JLabel icon_label;
    private javax.swing.JTextArea msg_txt;
    private javax.swing.JButton no_btn;
    private javax.swing.JButton ok_btn;
    private javax.swing.JButton yes_btn;
    // End of variables declaration//GEN-END:variables
}
